import 'package:safetymonitor/app/components/custom_select_function.dart';
import 'package:safetymonitor/app/components/toast_widget.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/http/http.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/data/entities/application/maintain_logs_request_entity.dart';
import 'package:safetymonitor/app/data/entities/application/maintenance_logs_entity.dart';
import 'package:safetymonitor/modules/home/device_manger/maintain_logs/controller.dart';

/// 添加/更新维护日志
class AddUpdateMaintainLogsController extends GetxController {
  /// 请求实体类
  MaintainLogsRequestEntity maintainLogsRequestEntity =
      MaintainLogsRequestEntity();

  ///编辑更新中-上个界面传入的数据体
  MaintenanceLogsEntity? maintenanceLogsEntity;

  @override
  void onInit() {
    super.onInit();
    if (Get.arguments['item'] != null) {
      //编辑更新
      maintenanceLogsEntity =Get.arguments['item'] as MaintenanceLogsEntity;
      //向编辑更新界面填充数据
      maintainLogsRequestEntity.operators = maintenanceLogsEntity?.operators ?? '';
      maintainLogsRequestEntity.maintenanceRecords = maintenanceLogsEntity?.maintenanceRecords ?? '';
      maintainLogsRequestEntity.operationTime = maintenanceLogsEntity?.operationTime.replaceAll('T', ' ') ?? '';
      //赋值id
      maintainLogsRequestEntity.id = maintenanceLogsEntity?.id ??0;
    }
    // 设备id
    maintainLogsRequestEntity.deviceId = Get.arguments['deviceId'] as int;
  }

  ///  操作时间选择
  void showDatePicker() {
    CustomSelectFunction.selectDateTime(
        onCallBack: (String value) {
          maintainLogsRequestEntity.operationTime = value;
          update();
        },
        helpText: '请选择操作时间');
  }

  ///创建/更新维护日志
  void confirm() async {
    if (maintainLogsRequestEntity.operators.trim().isEmpty) {
      AppToast.showWarning('请输入操作人员');
      return;
    }
    if (maintainLogsRequestEntity.operationTime.trim().isEmpty) {
      AppToast.showWarning('请选择操作时间');
      return;
    }
    if (maintainLogsRequestEntity.maintenanceRecords.trim().isEmpty) {
      AppToast.showWarning('请输入操作内容');
      return;
    }
    final Result res;
    if (maintenanceLogsEntity == null) {
      res = await HttpUtil()
          .generatePostRequest(
            Interface.createMaintenance,
            data: maintainLogsRequestEntity.toJson(),
          )
          .execute(isShowLoading: false, isShowToast: false);
    } else {
      res = await HttpUtil()
          .generatePutRequest(
            Interface.updateMaintenance,
            data: maintainLogsRequestEntity.toJson(),
          )
          .execute(isShowLoading: false, isShowToast: false);
    }

    res.onSuccess((result) async {
      if (result.data != null) {
        Get.back();
        AppToast.showSuccess(maintenanceLogsEntity == null?'创建成功~':'更新成功~');
        Get.find<MaintainLogsController>().globalKey.currentState?.onRefresh();
      }
    });
    res.onError((e) {
      AppToast.showError(e.message);
    });
  }
}
